﻿using MetroDataApi.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Linq;
using System.Web.Http;

namespace MetroDataApi.Controllers
{
    public class MonitorPointController : ApiController
    {
        // POST api/monitorpoint
        public string Post([FromBody]string value)
        {
            if (value == null)
            {
                return JsonConvert.SerializeObject(new { success = 0, err = "post参数不能为空,检查你的post方法" });
            }
            try
            {
                JObject jsonData = JObject.Parse(value);

                if (jsonData["value"] == null) return JsonConvert.SerializeObject(new { success = 0, err = "空值无法上传." });
                var point = jsonData.ToObject<result>();
                var prjId = jsonData["prjId"].ToObject<int>();
             
                var result = "";
                using (var db = new MetroDataDataContext())
                {
                    if (jsonData["psw"] == null || !MonitorPointController.uploadPswCheck(db, jsonData["psw"].ToString()))
                    {
                        return JsonConvert.SerializeObject(new { success = 0, err = "上传需要提供正确的授权码." });
                    }

                    result += MonitorPointController.uploadMonitorPoint(db, prjId, point, true);
                }

                return JsonConvert.SerializeObject(new { success = result.Length > 0 ? 0 : 1, err = result });
            }
            catch(Exception a)
            {
                return JsonConvert.SerializeObject(new { success = 0, err = a.Message });
            }

        }
        public static bool uploadPswCheck(MetroDataDataContext db, string psw)
        {
            var pp = db.UploadValidate.SingleOrDefault(p => p.Psw == psw);
            if (pp != null)
                return true;
            else
                return false;
        }
        public static string uploadMonitorPoint(MetroDataDataContext db , int prjId, result point, bool saveOnUpload = false)
        {
            var time = DateTime.Parse(point.time);

            //判断点和工程编号是否配置
            var errStr = MonitorPointController.preCheck(prjId, point.name, db);
            if (errStr.Length > 0)
                return errStr;
            else
            {
                var existed = db.MonitorPoints.SingleOrDefault(p => p.DateTime.Value == time && p.Name == point.name && p.PrjId == prjId);
                if (existed == null)
                {
                    db.MonitorPoints.InsertOnSubmit(new MonitorPoints()
                    {
                        DateTime = time,
                        Name = point.name,
                        PrjId = prjId,
                        Value = point.value
                    });
                }
                else
                {
                    existed.Value = point.value;
                }
                if (saveOnUpload == true)
                {
                    db.SubmitChanges();
                }

                return "";
            }
        }

        static private string preCheck(int prjId, string name,MetroDataDataContext db)
        {
            var prj = db.Projects.SingleOrDefault(p => p.PrjId == prjId);
            if (prj == null) return string.Format("无效的prjId:{0},该工程不存在", prjId); 

            var pt = db.MonitorPointsDictionary.SingleOrDefault(p => p.PrjId == prjId && p.Name == name);
            if (pt == null)
            {
                return string.Format("点'{0}'尚未配置,先配置后再上传", name);
            }
            return "";
        }


    }
}
